﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using ThoughtWorks.QRCode.Codec.Data;
using Newtonsoft.Json;
using System.IO;

namespace ProjectStudy
{
    /// <summary>
    /// 二维码 的摘要说明
    /// </summary>
    public class 二维码 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string str=context.Request["str"];
            using (MemoryStream ms = CreateQRCode(str))
            {
                context.Response.ContentType = "image/Gif";
                context.Response.BinaryWrite(ms.ToArray());
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        //生成二维码方法一
        private string CreateCode_Simple(string str, HttpContext context)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //System.Drawing.Image image = qrCodeEncoder.Encode("4408810820 深圳－广州 小江");
            System.Drawing.Image image = qrCodeEncoder.Encode(str);
            string filename = DateTime.Now.ToString("yyyymmddhhmmssfff").ToString() + ".jpg";
            string filepath =context.Server.MapPath("~\\UploadFiles\\") + filename;
            System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);

            fs.Close();
            image.Dispose();
            //二维码解码
            var codeDecoder = CodeDecoder(filepath);
            string path1 = urlToVirtual(context, filepath);
            
            return path1;
        }

        /// <summary>
        /// 二维码解码
        /// </summary>
        /// <param name="filePath">图片路径</param>
        /// <returns></returns>
        public string CodeDecoder(string filePath)
        {
            if (!System.IO.File.Exists(filePath))
                return null;
            Bitmap myBitmap = new Bitmap(System.Drawing.Image.FromFile(filePath));
            QRCodeDecoder decoder = new QRCodeDecoder();
            string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap));
            return decodedString;
        }

        /// 将物理路径转换成相对路径  
        /// </summary>  
        /// <param name="imagesurl1"></param>  
        /// <returns></returns>  
        private string urlToVirtual(HttpContext context, string imagesurl1)
        {
            //其实这里的tmpRootDir也等于tmpRootDir</span><span style="font-size:18px;">=Server.MapPath(</span><span style="font-size:18px;">"~/");              
            string tmpRootDir = context.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录  
            string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径  
            imagesurl2 = imagesurl2.Replace(@"\", @"/");
            return imagesurl2;
        }

        private MemoryStream CreateQRCode(string str)
        {
            MemoryStream ms = new MemoryStream();
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 4;
            qrCodeEncoder.QRCodeVersion = 8;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

            System.Drawing.Image image = qrCodeEncoder.Encode(str);
            Graphics g = Graphics.FromImage(image);
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

            g.Dispose();
            image.Dispose();

            return ms;
        }
  

    }
}